//
//  AudioEngine.h
//  MantuPlay
//
//  Created by Markus Sintonen on 7.10.2009.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <CoreFoundation/CoreFoundation.h>
#import <AudioToolbox/AudioQueue.h>
#import <AudioToolbox/AudioFile.h>
#import "Track.h"
#import "SynthesizeSingleton.h"
#import "AudioFrameBufferQueue.h"
#import "AudioOutputUnit.h"
#import "equalizer.h"

@protocol AudioEngineDelegate <NSObject>

@required
-(NSString*)nextTrackFilePath;
-(void)trackChanged;
@end

@interface AudioEngine : NSObject<AudioOutputUnitDelegate> {
	AudioOutputUnit *audioOutputUnit;
	
	AudioFileID	audioFile;
	AudioQueueRef audioQueue;
	AudioQueueBufferRef audioBuffer;
	AudioQueueBufferRef captureBuffer;
	UInt32 currentPacket;
	UInt32 numPacketsToRead;
	AudioStreamPacketDescription *audioPacketDescriptions;
	BOOL audioQueueFlushed;
	BOOL audioQueueDone;
	AudioTimeStamp ts;
	UInt32 reqFrames;
		
	AudioFrameBufferQueue *frameBufferQueue;
	Equalizer *equalizer;
	
	//NSArray *trackQueue;
	unsigned int currentTrackPlaying;
	
	BOOL updateAudioEngine;
	
	id<AudioEngineDelegate> delegate;
}

//@property (nonatomic, retain) NSArray *trackQueue;

SINGLETON_INTERFACE(AudioEngine, sharedAudioEngine)

-(void)setupTrackPlayerWithTrackPath:(NSString*)trackPath;
//-(void)playTrack;
-(void)setDelegate:(id<AudioEngineDelegate>)audioEngineDelegate;
@end

//Private methods
@interface AudioEngine ()

@property (nonatomic, assign) AudioStreamBasicDescription audioFormat;
@property (nonatomic, assign) AudioFileID	audioFile;
@property (nonatomic, assign) AudioQueueRef audioQueue;
@property (nonatomic, assign) AudioQueueBufferRef audioBuffer;
@property (nonatomic, assign) AudioQueueBufferRef captureBuffer;
@property (nonatomic, assign) AudioStreamPacketDescription *audioPacketDescriptions;
@property (nonatomic, assign) UInt32 currentPacket;
@property (nonatomic, assign) UInt32 numPacketsToRead;
@property (nonatomic, assign) BOOL audioQueueFlushed;
@property (nonatomic, assign) BOOL audioQueueDone;
@property (nonatomic, assign) AudioTimeStamp ts;
@property (nonatomic, assign) UInt32 reqFrames;

-(void)setupAudioEngine;
@end
